package com.bh.weapp.controller.customer;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.enums.BusinessType;
import com.bh.extend.domain.JcCustomer;
import com.bh.extend.domain.JcCustomerUser;
import com.bh.extend.service.IJcCustomerService;
import com.bh.extend.service.IJcCustomerUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 客户管理Controller
 *
 * @author slx
 * @date 2023-04-02
 */
@Api(tags = {"CUST客户管理API"})
@RestController
@RequestMapping("/api/cust/extend/jcCustomer")
public class JcCustomerCustApiController extends BaseController {

    @Autowired
    private IJcCustomerService jcCustomerService;

    @Autowired
    private IJcCustomerUserService jcCustomerUserService;

    /**
     * 修改保存客户管理绑定
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("保存客户管理绑定")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/bind/{code}")
    public AjaxResult bindSave(@PathVariable String code) {
        JcCustomer jcCustomer = jcCustomerService.selectJcCustomerByCode(code);
        if (jcCustomer == null) {
            return error("请输入正确的合同号！");
        }
        JcCustomerUser user = new JcCustomerUser();
        user.setUserId(getUserId());
        user.setCustomerId(jcCustomer.getId());
        return toAjax(jcCustomerUserService.insertJcCustomerUser(user));
    }
}
